#!/usr/bin/env python
# -*- coding:utf-8 -*-
import configparser
import os
import sys

__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
sys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))
f = r'E:\zentao_notify\vpTransdoor\setting.ini'
config_file = configparser.RawConfigParser()
config_file.read(f, encoding='UTF8')

obj_col = []
# 数据列方法名称
col = [
    "SA_0", "SA_1", "SA_2", "SA_3", "SA_4", "SA_5", "Total_min_SA", "Running_SA", "SA_too_low", "Year", "Rebate",
    "EGC_premium", "Cumulative_premium", "Annual_withdrawal", "Remain_", "Balance", "EGC_premium", "EGC_CV",
    "Adjusted_EGC_CV", "A_EGC_CV_1", "A_EGC_CV_2", "A_EGC_CV_3", "A_EGC_CV_4", "A_EGC_CV_5", "Total_A_EGC_CV",
    "re_adjusted_CV", "Balance_before_withdraw", "Retired", "Income", "Inflation_factor", "Inflated_income",
    "Withdrawal", "Cashflow"
]
# 数据列的excel坐标
col2 = [
    "K60", "L60", "M60", "N60", "O60", "P60", "Q60", "R60", "S60", "T60", "U60", "V60", "W60", "X60", "Y60", "Z60",
    "AA60", "AB60", "AC60", "AD60", "AE60", "AF60", "AG60", "AH60", "AI60", "AJ60", "AK60", "AL60", "AM60", "AN60",
    "AO60", "AP60", "AQ60"
]
# 数据名称
col3 = [
    "SA 0", "SA 1", "SA 2", "SA 3", "SA 4", "SA 5", "Total min SA", "Running SA", "SA too low", "Year", "Rebate",
    "EGC premium", "Cumulative premium", "Annual withdrawal ", "Remain ", "Balance", "EGC premium", "EGC CV",
    "Adjusted EGC CV", "A. EGC CV 1", "A. EGC CV 2", "A. EGC CV 3", "A. EGC CV 4", "A. EGC CV 5", "Total A. EGC CV",
    "re_adjusted CV", "Balance before withdraw", "Retired", "Income", "Inflation factor", "Inflated income",
    "Withdrawal", "Cashflow"
]
# 数据使用的EXCEL公式
col4 = [
    "=$C$20", "=IF($C$7>0,IF(($T62-$C$6+1)>0,$C$20*(1+$C$8)^1,0),0)",
    "=IF($C$7>1,IF(($T62-$C$6*2+1)>0,$C$20*(1+$C$8)^2,0),0)", "=IF($C$7>2,IF(($T62-$C$6*3+1)>0,$C$20*(1+$C$8)^3,0),0)",
    "=IF($C$7>3,IF(($T62-$C$6*4+1)>0,$C$20*(1+$C$8)^4,0),0)", "=IF($C$7>4,IF(($T62-$C$6*5+1)>0,$C$20*(1+$C$8)^5,0),0)",
    "=(ROUNDDOWN(MIN(T62,$C$6*(1+$C$7)-1)/$C$6,0)+1)*$C$45", "=SUM($K62:$P62)*$Y61", "=$R62<$Q62", "=B62-$C$3", "0",
    "=IF(B62<102,IF(AND($C$18,A62<$C$6),IF(A62=0,AA62*$C$6-$C$19,0),AA62)*$C$33-U62,0)", "=W61+V62",
    "=IF(AJ62>0,(E62/$C$33)/AJ62,0)", "=Y61-X62", "=Y62*AJ62",
    "=IF(B62-$C$3<($C$6*($C$7+1)),$C$40*(1+$C$8)^ROUNDDOWN((B62-$C$3)/$C$6,0),0)",
    "=VLOOKUP(B62-$C$3+1,data4!$A$4:$FL$124,$C$38,TRUE)*$C$39", "=AB62",
    "=IF($C$7>0,IF(($T62-$C$6+1)>0,VLOOKUP($B62-$C$6,$B$62:$AC$162,$C$42,TRUE)*(1+$C$8)^1,0),0)",
    "=IF($C$7>1,IF(($T62-$C$6*2+1)>0,VLOOKUP($B62-$C$6*2,$B$62:$AC$162,$C$42,TRUE)*(1+$C$8)^2,0),0)",
    "=IF($C$7>2,IF(($T62-$C$6*3+1)>0,VLOOKUP($B62-$C$6*3,$B$62:$AC$162,$C$42,TRUE)*(1+$C$8)^3,0),0)",
    "=IF($C$7>3,IF(($T62-$C$6*4+1)>0,VLOOKUP($B62-$C$6*4,$B$62:$AC$162,$C$42,TRUE)*(1+$C$8)^4,0),0)",
    "=IF($C$7>4,IF(($T62-$C$6*5+1)>0,VLOOKUP($B62-$C$6*5,$B$62:$AC$162,$C$42,TRUE)*(1+$C$8)^5,0),0)", "=SUM(AC62:AH62)",
    "=IF($S62,$AJ61*(1+Notes!$B$3),$AI62)", "=AJ62*Y61", "=(B62>=$F$5)",
    "=IF(AL62,IF($F$8>0,AND($B62<$F$3,($B62-$F$5)<$F$8),$B62<$F$3),FALSE)", "=IF(AL62,(1+$F$7)^(B62-$F$5),1)",
    "=IF($AM62,$F$6/$C$33*$AN62,0)", "=MIN(AO62+D62/$C$33,AK62)", "=IF($F$3>$C$3,IF($B62<$F$3,$E62-$C62,$E62+$G62),0)"
]
col5 = [
    "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE",
    "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "FALSE",
    "FALSE", "TRUE", "TRUE", "TRUE", "TRUE"
]
START = 11
END = 43
DECIMAL_NUM = 2

CALSS_NAME = "Egc"


def makeTabCalStd():
    arry_method = []
    arry_call = []
    try:
        p_path = config_file["excelobj"]["outPath"] + f"\\{str(CALSS_NAME).upper()}\\"
        str_out_file_path = p_path + CALSS_NAME + "TabCalStd.py"

        print("METHOD:" + "#" * 100)
        for _idx in range(len(col)):
            arry_method.append(" " * 4 + f"# 计算 {col3[_idx]}")
            arry_method.append(" " * 4 + f"# Return: {col3[_idx]} {col2[_idx]}: {col3[_idx]}")
            arry_method.append(" " * 4 + f"# Params:\n")
            arry_method.append(" " * 4 + f"def get_{col[_idx]}(self,):")
            arry_method.append(" " * 8 + f"# 参考计算公式")
            arry_method.append(" " * 8 + f"# {col4[_idx]}")
            arry_method.append(" " * 8 + f"return 0")
            arry_method.append("\n")
        print("NAMING:" + "#" * 100)
        for _idx in range(END - START):
            if col5[_idx] == "TRUE":
                print(" " * 20 + f"one_{col[_idx]}_res = 0")
            else:
                print(" " * 20 + f"one_{col[_idx]}_res = ''")
        print("CALLING:" + "#" * 100)
        for _idx in range(END - START):
            arry_call.append(" " * 20 + f"# Col {_idx + START} 计算 {col3[_idx]}")
            arry_call.append(" " * 20 + f"one_{col[_idx]} = calStd.get_{col[_idx]}()")
            if col5[_idx] == "TRUE":
                arry_call.append(
                    " " * 20 + f"single_row_data[\"one_{col[_idx]}_res\"] = round(one_{col[_idx]}, {DECIMAL_NUM})")
            else:
                arry_call.append(" " * 20 + f"single_row_data[\"one_{col[_idx]}_res\"] = one_{col[_idx]}")
        print("END:" + "#" * 100)

        str_file_path = config_file["excelobj"]["basePath"]
        _content = ''
        with open(str_file_path, 'r', encoding='utf8') as _f:
            _content = _f.read()
            _content = str(_content).replace("###AclassName###", CALSS_NAME)
            _content = str(_content).replace("###className###", str(CALSS_NAME).lower())
            _content = str(_content).replace("## METHOD ##", "\n".join(arry_method))
            _content = str(_content).replace("## CALLING ##", "\n".join(arry_call))
            print("Make file:\n" + _content)
            _f.close()

        with open(str_out_file_path, 'a', encoding='utf8') as _f:
            _f.write(_content)
            _f.write('\n')
            _f.close()
    except Exception as e:
        print(e)


def makeTabTop():
    arry_method = []
    arry_naming = []
    try:
        p_path = config_file["excelobj"]["outPath"] + f"\\{str(CALSS_NAME).upper()}\\"
        str_out_file_path = p_path + CALSS_NAME + "TabTop.py"
        if not os.path.exists(p_path):
            os.makedirs(p_path)
        print("METHOD:" + "#" * 100)
        for _idx in range(len(col)):
            arry_method.append(" " * 4 + f"# 计算 {col3[_idx]}")
            arry_method.append(" " * 4 + f"# Return: {col3[_idx]} {col2[_idx]}: {col3[_idx]}")
            arry_method.append(" " * 4 + f"# Params:\n")
            arry_method.append(" " * 4 + f"def get_{col[_idx]}(self,):")
            arry_method.append(" " * 8 + f"# 参考计算公式")
            arry_method.append(" " * 8 + f"# {col4[_idx]}")
            arry_method.append(" " * 8 + f"return 0")
            arry_method.append("\n")
        print("NAMING:" + "#" * 100)
        for _idx in range(len(col)):
            if col5[_idx] == "TRUE":
                arry_naming.append(" " * 8 + f"\"{col[_idx]}\" : 0,")
            else:
                arry_naming.append(" " * 8 + f"\"{col[_idx]}\" : '',")

        print("END:" + "#" * 100)

        str_file_path = config_file["excelobj"]["topPath"]
        _content = ''
        with open(str_file_path, 'r', encoding='utf8') as _f:
            _content = _f.read()
            _content = str(_content).replace("###AclassName###", CALSS_NAME)
            _content = str(_content).replace("## METHOD ##", "\n".join(arry_method))
            _content = str(_content).replace("## NAMING ##", "\n".join(arry_naming))
            print("Make file:\n" + _content)
            _f.close()
        with open(str_out_file_path, 'a', encoding='utf8') as _f:
            _f.write(_content)
            _f.write('\n')
            _f.close()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    makeTabTop()
    makeTabCalStd()
